module MainHandler (
        MainHandler, 
        createMainHandler, -- createMainHandler :: MainHandler
        addCustomerToHandler, -- addCustomerToHandler :: MainHandler -> Customer -> MainHandler
        addBaseHaircutToHandler, -- addBaseHaircutToHandler :: MainHandler -> Haircut -> MainHandler
        getCustomers, -- getCustomers :: MainHandler -> [Customer]
        getBaseHaircuts, -- getBaseHaircuts :: MainHandler -> [Haircut]
        initCustomersDataBase, --initCustomersDataBase :: MainHandler
        initHaircutDataBase, -- initCustomersDataBase :: MainHandler
        setNewCustomerList, -- setNewCustomerList :: MainHandler -> [Customer] -> MainHandler
        getCustomerById,
        setHaircutsList -- :: MainHandler -> [Haircut] -> MainHandler
) where
import Customer
import Haircut
import PerformedHaircut
import Data.IORef
import WindowUtils

{-
AUTOR:Gaston Charkiewicz
DESCRIPCION: base de datos del sistema. Atada con alambre, hasta que se haga una 
"pasada monadica".
OBSERVACIONES: utiliza una lista para almacenarlos.
-}

data MainHandler = MkMainHandler [Haircut] [Customer]

createMainHandler :: MainHandler
createMainHandler = MkMainHandler [] []

addCustomerToHandler :: MainHandler -> Customer -> MainHandler
addCustomerToHandler (MkMainHandler gHaircuts []) customerToAdd   = 
        MkMainHandler gHaircuts [customerToAdd]
addCustomerToHandler (MkMainHandler gHaircuts (xs)) customerToAdd = 
        MkMainHandler gHaircuts (customerToAdd:xs)
        
addBaseHaircutToHandler :: MainHandler -> Haircut -> MainHandler
addBaseHaircutToHandler (MkMainHandler [] customers) theHaircut    = MkMainHandler [theHaircut] customers  
addBaseHaircutToHandler (MkMainHandler (ghs) customers) theHaircut = MkMainHandler (theHaircut:ghs) customers

getCustomers :: MainHandler -> [Customer]
getCustomers (MkMainHandler _ theListOfCustomers) = theListOfCustomers

getBaseHaircuts :: MainHandler -> [Haircut]
getBaseHaircuts (MkMainHandler hisHaircuts hisCustomers) = hisHaircuts

----------------------------------------------------------------------
---------FUNCIONES PARA LLENARLA DE PRUEBAS ----------------------
----------------------------------------------------------------------

initCustomersDataBase :: MainHandler
initCustomersDataBase = addCustomerToHandler (addCustomerToHandler  createMainHandler 
                   (addPerformedHaircut (createCustomer "PEPE")
                   (createPerformedHaircut (createHaircut "tintura" "aplicar tintura") "todo bien") )
                   )  
                   (createCustomer "SUPER PEPE")
                   
                   
initHaircutDataBase :: MainHandler -> MainHandler
initHaircutDataBase aMainHandler =  addBaseHaircutToHandler aMainHandler (createHaircut "Afro" "Un afro re loco")

setNewCustomerList :: MainHandler -> [Customer] -> MainHandler
setNewCustomerList (MkMainHandler hc _) customers = (MkMainHandler hc customers) 

getCustomerById int db = do
                index <- int
                cdb <- readIORef db
                return (searchByIndex index (getCustomers cdb))
                
setHaircutsList :: MainHandler -> [Haircut] -> MainHandler
setHaircutsList (MkMainHandler _ customers) newHcs = MkMainHandler newHcs customers